﻿Imports TBCC.UtilityMobile
Imports TBCC.UserManage


Public Class TerminalForm

#Region " 参数节点名称 "

    Private Const T_SHOW_DATA As String = "tShowData"
    Private Const T_CFG_RECORDCTRL As String = "tCfgRecordCtrl"
    Private Const T_CFG_RECORDINTVAL As String = "tCfgRecordIntval"
    Private Const T_CFG_SYSTIME As String = "tCfgSysTime"

    Private Const T_CFG_RTITIME As String = "CarRtiTime"  '基准时间


    Private Const T_CFG_MODEFYPAC As String = "modefyPac"

    Private Const T_CFG_NET As String = "tCfgNet"
    '  Private Const T_CFG_TEMPAI As String = ""
    Private Const T_CFG_SRVADDR As String = "tCfgSrvAddr"
    Private Const T_CFG_NETPARA As String = "tCfgNetPara"
    Private Const T_CFG_GPRSPARA As String = "tCfgGPRSPara"

    Private Const T_CFG_IOPORT As String = "tCfgIOPort"
    Private Const T_CFG_ALARMPHONE As String = "tCfgAlarmPhone"

    'Private Const T_CFG_REVISE As String = "tCfgRevise"
    Private Const T_CFG_REVISEVALUE As String = "tCfgReviseValue"
    'Private Const T_CFG_SHOWAI As String = "tCfgShowAI"

    Private Const T_CFG_ZERO As String = "zeromodefy"

    Private Const T_CFG_SHOWAI As String = "AIZeroRevise"
    Private Const T_CFG_CARTEST As String = "cartest"
    Private Const T_CFG_CARPSET As String = "CarPSet"
    Private Const T_CFG_CARSTARTSTOPCON As String = "CarStartStopCon"
    Private Const T_CFG_CARSTARTSPACE As String = "CarStartStopSpace"

    Private Const T_CFG_CARALARMlIMIT As String = "CarAlarmLimit"

#End Region

#Region " 构造函数 "

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        SetControlEnabled(False)
    End Sub

#End Region

    '是否连接终端
    Public Shared IsConnected As Boolean = False

    '当前节点名称
    Private _CurrentNodeName As String = String.Empty

    'tvConfigList_AfterSelect和tvConfigList_NodeMouseClick的同步锁
    Private _SelectFormLocker As New Object()

    '是否已有终端
    Public HasOldPrj As Boolean

    '是否新增终端
    Public HasNewPrj As Boolean

    '当前连接终端的ProjectItem
    Public PI As ProjectItem

    Private Sub TerminalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CenterToParent()

        CheckUserRight()

        ' LoadForm(Of TerminalConnectForm)(Me)

    End Sub

    Private Sub TerminalForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing


        Try
            COMHelper.Instance.Close()
        Catch ex As Exception
            Debug.WriteLine(ex.Message)

        End Try


        '  ' If HasNewPrj = True Then
        ' Me.DialogResult = Windows.Forms.DialogResult.OK
        ' Else
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        ' End If
        CloseForm()
    End Sub

    Private Sub tvConfigList_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvConfigList.AfterSelect

        SelectForm(e.Node)

    End Sub

    Private Sub tvConfigList_NodeMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvConfigList.NodeMouseClick

        SelectForm(e.Node)

    End Sub



   

    

    Private Sub mnuStartupConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuStartupConfig.Click

        CloseForm()

        Me.grpMain.Text = "启/停记录配置"

        'LoadForm(Of TerminalDataUploadForm)(Me) 'TODO

    End Sub

    Private Sub mnuHisDataUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHisDataUpload.Click, tsbHisDataUpload.Click

        CloseForm()

        Me.grpMain.Text = String.Empty '"历史数据上传"

        'LoadForm(Of TerminalDataUploadForm)(Me) 'TODO
        Dim frm As New TerminalDataUploadForm()
        frm.SetMyParentForm(Me)
        frm.ShowDialog()

    End Sub

    Private Sub mnuHisAlarmUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHisAlarmUpload.Click

        CloseForm()

        Me.grpMain.Text = "历史报警上传"

        'LoadForm(Of AlarmDataUploadForm)()

    End Sub

    Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClose.Click
        Me.Close()
    End Sub

    '加载参数窗体
    'Private Sub LoadForm(Of T As {TerminalChildForm, New})()
    '    LoadForm(Of T)(Nothing)
    'End Sub
    Private Sub LoadForm(Of T As {TerminalChildForm, New})(ByVal ParentForm As Form)
        Dim frm As New T()
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.Dock = DockStyle.Fill
        frm.Parent = Me.pnlMain
        If ParentForm IsNot Nothing Then frm.SetMyParentForm(ParentForm)
        frm.Show()
    End Sub

    '关闭参数窗体
    Private Sub CloseForm()
        For Each c As Control In Me.pnlMain.Controls
            If TypeOf c Is Form Then
                CType(c, Form).Close()
            End If
        Next
    End Sub

    '检测是否有窗体
    Private Function HasForm() As Boolean
        For Each c As Control In Me.pnlMain.Controls
            If TypeOf c Is Form Then
                Return True
            End If
        Next
        Return False
    End Function

    '根据选择节点显示配置窗口
    Private Sub SelectForm(ByVal node As TreeNode)
        If System.Threading.Monitor.TryEnter(_SelectFormLocker, 1) = True Then

            Try

                'If _CurrentNodeName.Equals(node.Name) = True Then
                '    Exit Sub
                'End If

                Select Case node.Name
                    Case T_SHOW_DATA
                        CloseForm()
                        LoadForm(Of TerminalShowForm)(Me)
                        Me.grpMain.Text = node.Text

                   


                    Case T_CFG_MODEFYPAC
                        CloseForm()
                        LoadForm(Of NetAddressConfigForm)(Me)
                        Me.grpMain.Text = node.Text

                    Case T_CFG_NET


                    Case T_CFG_CARPSET




                    

                  

                   



                End Select

                _CurrentNodeName = node.Name

            Finally
                System.Threading.Monitor.Exit(_SelectFormLocker)
            End Try

        End If
    End Sub

    '检测用户权限
    Private Sub CheckUserRight()
        Dim gp As System.Security.Principal.GenericPrincipal
        gp = My.User.CurrentPrincipal

        If gp.IsInRole(RoleClass.GetRolename(RoleType.Admin)) Then
            '管理员
            Me.mnuPrjActive.Enabled = False     '数据记录启动/停止
            Me.mnuPrjActive.Visible = False
            Me.tsbPrjActive.Enabled = False
            Me.tsbPrjActive.Visible = False
            Me.tsbPrjStop.Enabled = False
            Me.tsbPrjStop.Visible = False
            Me.mnuRecordCfg.Enabled = False     '数据记录间隔设置
            Me.mnuRecordCfg.Visible = False
            Me.tsbRecordCfg.Enabled = False
            Me.tsbRecordCfg.Visible = False
            'Me.mnuShowAI.Enabled = False        '显示AI信号值
            'Me.mnuShowAI.Visible = False
            Me.mnuStartupConfig.Enabled = False     '启停记录配置
            Me.mnuStartupConfig.Visible = False
            Me.mnuHisDataUpload.Enabled = False     '数据记录上传
            Me.mnuHisDataUpload.Visible = False
            Me.tsbHisDataUpload.Enabled = False
            Me.tsbHisDataUpload.Visible = False
            Me.mnuHisAlarmUpload.Enabled = False    '报警记录上传
            Me.mnuHisAlarmUpload.Visible = False

            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_MODEFYPAC)     'PAC地址修改
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_SYSTIME)        '系统时间设置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_NET)            '网络参数配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_IOPORT)         'IO端口配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_ALARMPHONE)     '报警号码配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_REVISEVALUE)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_ZERO)
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_SHOWAI)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_CARTEST)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_CARPSET)

        End If

        If gp.IsInRole(RoleClass.GetRolename(RoleType.NormalLevel)) Then
            '普通用户
            Me.mnuRecordCfg.Enabled = False     '数据记录间隔设置
            Me.mnuRecordCfg.Visible = False
            Me.tsbRecordCfg.Enabled = False
            Me.tsbRecordCfg.Visible = False

            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_MODEFYPAC)     'PAC地址修改
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_SYSTIME)        '系统时间设置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_NET)            '网络参数配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_IOPORT)         'IO端口配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_ALARMPHONE)     '报警号码配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_REVISEVALUE)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_ZERO)
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_CARTEST)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_CARPSET)

        End If

        If gp.IsInRole(RoleClass.GetRolename(RoleType.MiddleLevel)) Then
            '中级用户

            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_SYSTIME)        '系统时间设置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_NET)            '网络参数配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_IOPORT)         'IO端口配置
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_REVISEVALUE)    '温湿度偏差值修正
            Me.tvConfigList.Nodes.RemoveByKey(T_CFG_CARPSET)

        End If

        If gp.IsInRole(RoleClass.GetRolename(RoleType.HighLevel)) Then
            '高级用户

        End If
    End Sub

    Public Sub SetControlEnabled(ByVal flag As Boolean)
        If Me.InvokeRequired Then
            Me.Invoke(New Action(Of Boolean)(AddressOf _SetControlEnabled), flag)
        Else
            _SetControlEnabled(flag)
        End If
    End Sub
    Private Sub _SetControlEnabled(ByVal flag As Boolean)
        Me.tvConfigList.Enabled = flag
        Me.mnuPrjActive.Enabled = flag
        Me.tsbPrjActive.Enabled = flag
        Me.tsbPrjStop.Enabled = flag
        Me.mnuRecordCfg.Enabled = flag
        Me.tsbRecordCfg.Enabled = flag
        Me.mnuShowAI.Enabled = flag
        Me.mnuStartupConfig.Enabled = flag
        Me.mnuHisDataUpload.Enabled = (HasNewPrj OrElse HasOldPrj)
        Me.tsbHisDataUpload.Enabled = Me.mnuHisDataUpload.Enabled

        'Me.mnuHisAlarmUpload.Enabled = flag
    End Sub

    Public Sub SetText(ByVal str As String, ByVal isAppend As Boolean)
        Dim obj(1) As Object
        obj(0) = str
        obj(1) = isAppend

        If Me.InvokeRequired Then
            Me.Invoke(New Action(Of Object)(AddressOf _SetText), CType(obj, Object))
        Else
            _SetText(CType(obj, Object))
        End If
    End Sub
    Private Sub _SetText(ByVal obj As Object)
        Dim str As String = obj(0)
        Dim isAppend As Boolean = obj(1)

        If isAppend = True Then
            Me.Text &= str
        Else
            Me.Text = str
        End If
    End Sub

    Public Sub SetToolsBarPrjActive(ByVal flag As Boolean)
        If Me.InvokeRequired Then
            Me.Invoke(New Action(Of Boolean)(AddressOf _SetToolsBarPrjActive), flag)
        Else
            _SetToolsBarPrjActive(flag)
        End If
    End Sub
    Public Sub _SetToolsBarPrjActive(ByVal flag As Boolean)
        Me.tsbPrjStop.Enabled = Not flag
        Me.tsbPrjStop.Visible = Not flag
        Me.tsbPrjActive.Enabled = flag
        Me.tsbPrjActive.Visible = flag

    End Sub


   

    Private Sub msMain_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles msMain.ItemClicked

    End Sub
End Class